`timescale 1ns / 1ps
/* Module for vga display:

Here  Level 1 enemies :enemy 1=  octups looking like enemy
							  enemy 2= yellow colored UFO
							  enemy 3=laughing devel
							  enemy 4= barrel like enemy
							  enemy 5 = sitting enemy
							  enemy 6 = viloet colored enemy







*/
//////////////////////////////////////////////////////////////////////////////////

module vga_2(	collision,
				clk_old, 
				enable1,
				enable2,  
				enable4,
				enable5,
				enable6,
				enable7,
				enable8,
				enable9,
				enable10,
				enable11,
				enable12,
				enable13,
				enable14,
				enable15,
				enable16,
				enable17,
				enable18,
				red,
				green,
				blue,
				HS,
				VS,
				a,
				lives,
				level1,
				level2,
				level3,
				lvl1,
				lvl2,
				start,
				X1,
				Y1,
				X2,
				Y2,
				X4,
				Y4,
				X5,
				Y5,
				X6,
				Y6,
				X7,
				Y7,
				X8,
				Y8,
				X9,
				Y9,
				X10,
				Y10,
				X11,
				Y11,
				X12,
				Y12,
				X13,
				Y13,
				X14,
				Y14,
				X15,
				Y15,
				X16,
				Y16,
				X17,
				Y17,
				X18,
				Y18,
				X19,
				Y19,
				X20,
				Y20,
				game_over);
				
				input game_over;
				input start;
				input wire clk_old;
				input enable1;											
				input enable2;										
				input enable4;
				input enable5;
				input enable6;
				input enable7;
				input enable8;
				input enable9;
				input enable10;
				input enable11;
				input enable12;
				input enable13;
				input enable14;
				input enable15;
				input enable16;
				input enable17;
				input enable18;
				input [10:0]    			 X1,Y1,
												 X2,Y2,
												 X4,Y4,
												 X5,Y5,
												 X6,Y6,
												 X7,Y7,
												 X8,Y8,
												 X9,Y9,
												 X10,Y10,
												 X11,Y11,
												 X12,Y12,
												 X13,Y13, 
												 X14,Y14,
												 X15,Y15,
												 X16,Y16, 
												 X17,Y17,
												 X18,Y18, 
												 X19,Y19,
									          X20,Y20;
						input  [15:0]      a;
						input  [3:0]       lives;
						input              level1,
											    level2,
											    level3,
											    lvl1, 
											    lvl2;
			   output reg [2:0]    		 red;
				output reg [2:0]         green; 
				output  reg	 [1:0]        blue;
				output HS;
				output VS;
				reg [40:0] count=0;
				reg clk60=0;
				wire condition;
				wire signed [10:0] hcounter1,
				                   hcounter2,
										 hcounter3;
				assign   hcounter1=hcounter-50;
				assign   hcounter2=hcounter-100;
				assign   hcounter3=hcounter-150;
				
				assign condition=~game_over && ~lvl1 && ~lvl2 && ~start;
				
				wire               vgadisp1,
										 vgadisp2,
										 vgadisp3,
										 vgadisp4, 
										 le,
										 ve,
										 el, 
										 LvN,
										 sc5,
										 sc4,
										 sc3,
										 sc2,
										 sc1;
				wire               vgadisp5,
									    vgadisp6,
									    vgadisp7, 
									    vgadisp8, 
									    vgadisp9,
										 vgadisp10,
										 vgadisp11, 
										 vgadisp12,
										 vgadisp13, 
										 vgadisp14,
										 vgadisp15, 
										 vgadisp16, 
										 vgadisp17, 
										 vgadisp18,
										 vgadisp19,
										 life1,
										 life2, 
										 life3, 
										 life4;
				wire signed [10:0] hcounter;
				wire signed [10:0] vcounter;
				wire 					   blank;
				wire [14:0]          addra;
				wire [7:0]           data;
				input                collision;
				wire [12:0]          addrc;
				wire [7:0]           data2;
				wire [14:0]          addrd;
				wire [7:0]           data3;
				wire [12:0]          addre;
				wire [7:0]           data4;
				wire [10:0]          addrf;
				wire [7:0]           data5;
				wire [10:0]          addrg;
				wire [7:0]           data6;
				wire [11:0]          addrh;
				wire [7:0]           data7;
				wire [11:0]          addri;
				wire [7:0]           data8;
				wire [11:0]          addrj;
				wire [7:0]           data9;
				wire                 TitleImage1, 
				                     TitleImage2;
				wire 					   sprite1,
											sprite2,
											sprite3, 
											sprite4, 
											sprite5, 
											sprite6, 
											sprite7, 
											sprite8, 
											sprite9,
											sprite10,
											sprite11;
				reg signed [11:0]  	Xpos,
											Xposnext, 
											Xposnext1,
											Xposnext2;
				initial begin
											Xpos<=0;
											Xposnext<=234;
											Xposnext1<=468;
											Xposnext2<=702;
				end
				assign sprite1=	hcounter>=X4 && hcounter<X4+49 && vcounter>=Y4 && vcounter<Y4+39;
				assign sprite2=	(hcounter>=Xpos && hcounter<Xpos+237 && vcounter>=342 && vcounter<479) || 
										(hcounter>=Xposnext && hcounter<Xposnext+237 && vcounter>=342 && vcounter<479)  ||
										(hcounter>=Xposnext1 && hcounter<Xposnext1+237 && vcounter>=342 && vcounter<479) ||
										(hcounter>=Xposnext2 && hcounter<Xposnext2+233 && vcounter>=342 && vcounter<479);
				assign sprite4=	hcounter>=X1 && hcounter<X1+47 && vcounter>=Y1 && vcounter<Y1+44;
				assign sprite5=	hcounter>=X2 && hcounter<X2+46 && vcounter>=Y2 && vcounter<Y2+47;
				assign sprite6=	hcounter>=X5 && hcounter<X5+49 && vcounter>=Y5 && vcounter<Y5+42;
				assign sprite7=	hcounter>=X6 && hcounter<X6+41 && vcounter>=Y6 && vcounter<Y6+31;
				assign sprite8=	hcounter>=X7 && hcounter<X7+48 && vcounter>=Y7 && vcounter<Y7+42;
				assign sprite9=	hcounter>=X8 && hcounter<X8+51 && vcounter>=Y8 && vcounter<Y8+50;
				assign sprite10= hcounter>= X9 && hcounter< X9+51 && vcounter>= Y9 && vcounter< Y9+50;
	
	// clock divider converting 100  MHz to 25 MHz
	slowclk1 Q1( clk_old,
					pixel_clk);
	
	// vga controller generating hcounter & vcounter for 640x480 pixels
	vga_controller Q2(pixel_clk, 
							HS,
							VS,
							hcounter,
							vcounter,
							blank);
							
	//module for score, life and level display
	scoreDisplay SC({life1, life2, life3, life4}, 
						lives,
						{le,ve,el,LvN},
						{level3,level2,level1}, 
						{sc5,sc4,sc3,sc2,sc1},
						a, 
						hcounter,
						vcounter,
						blank);
						
	// address calculation module for main character
	
	addrcalc_mc v7 (hcounter,
						vcounter,
						blank,
						addrc,
						X4,
						Y4);
						
	// sprite containing main character
	mc v8          (pixel_clk,
						addrc,
						data2);
						
	//address calculation for background
	
	backaddr_calc v9   (Xpos,
						    hcounter, 
						    vcounter,
						    blank,
							 addrd,
							 Xposnext,
							 Xposnext1,
							 Xposnext2);
	
	// sprite containing background
	
	backgrnd v6        (pixel_clk,
					        addrd,
							  data3);
	
	// address calculation for enemies of first level
	
	enemy_addrcal v11(enable1,
							enable2,
							hcounter,
							vcounter,
							blank,
							addre,
							X1,
							X2,
							Y1,
							Y2);
	// sprite containing enemy 1 and enemy 2	
	
	enemies v10			(pixel_clk,
							addre,
							data4);
	
	// address calculation for sprite of collision
	
	addr_collision v13(collision,
							hcounter, 
							vcounter,
							blank,
							addrf,
							X5,
							Y5);
							
	// sprite containing collision
	
	collision v12    (pixel_clk,
					      addrf,
					      data5);
							
	// address calculation for enemy of level2
	
	addrcal_enemy4 e5(enable4,
	                  hcounter, 
							vcounter,
							blank,
							addrg,
							X6,
	    					Y6);
	
	// sprite containing enemy 3
	
	enemy4 e6        (pixel_clk,
							addrg,
							data6);
	
	// address calculation for enemy of level2
	
	addrcal_enemy5 e7(enable5,
							hcounter, 
							vcounter,
							blank,
							addrh,
							X7,
							Y7);
		
	// sprite containing enemy 4
	
	
	enemy5 e8		  (pixel_clk,
							addrh,
							data7);
	// address calculation for enemy of level3
							
	addrcal_enemy7 e9(enable6,
							hcounter,
							vcounter,
							blank,
							addri,
							X8,
							Y8);
	
	// sprite containing enemy 5
	enemy7 e10			(pixel_clk,
							addri,
							data8);
	
	// address calculation for enemy of level3
	addrcal_enemy8 e11(enable7,
							hcounter,
							vcounter,
							blank,
							addrj, 
							X9,
							Y9);
	// sprite containing enemy 6						
	enemy8 e12			(pixel_clk,
							addrj,
							data9);
	
	// module for display of bullets for each level and for level transitions display
	
	bulletdisplay_final e13 (enable8,
									enable9,
									enable10, 
									enable11, 
									enable12,
									enable13,
									enable14, 
									enable15, 
									enable16, 
									enable17,
									enable18,
									game_over, 
									lvl1,
									lvl2, 
									enable20,
									vgadisp5, 
									vgadisp6,
									vgadisp7,
									vgadisp8,
									vgadisp9,
									vgadisp10, 
									vgadisp11,
									vgadisp12,
									vgadisp13, 
									vgadisp14, 
									vgadisp15, 
									vgadisp16,
									vgadisp17, 
									vgadisp18,
									vgadisp19, 
									hcounter, 
									vcounter,
									blank,
									X10,
									Y10,
									X11,
									Y11,
									X12,
									Y12,
									X13,
									Y13,
									X14,
									Y14, 
									X15,
									Y15,
									X16,
									Y16,
									X17,
									Y17,
									X18,
									Y18,
									X19,
									Y19,
									X20,
									Y20,
									X22,
									Y22 );
									
	drawTitle e14				 (start, 
										hcounter, 
										vcounter, 
										blank, 
										{TitleImage1, TitleImage2});
	
	// at every positive edge the signals will enable display of corresponding sprites
	// variables sprite1, sprite 2.. sprite10 are used to ensure the region present in the corresponding sprite is
	//read only once
	
		always@(posedge pixel_clk) 
			
			begin


							if(count==222500) begin
							count<=0;
							clk60<=~clk60;
							end
							else
							count<=count+1;

								 if((sc5 || sc4 || sc3 || sc2 || sc1) && ~collision && condition) begin
									red=3'b001;
									green=3'b111;
									blue=2'b01;
									end
								else if( (life4 || life3 || life2 || life1) && condition ) begin
										red=3'b111;
										green=3'b100;
										blue=2'b10;
										end
								else if ((le||ve||el||LvN) && condition) begin
										red=3'b101;
										green=3'b001;
										blue=2'b01;
										end
										
								else if ((sprite1 && ~collision && data2!={8'b0}) && condition) begin
										{red,green,blue}=data2;
										end
								else if((sprite4 && data4!={8'b0}) && condition) begin
										{red,green,blue}=data4;
										end
								else if((sprite5 && data4!={8'b0}) && condition) begin
										{red,green,blue}=data4;
										end
										
								else if((sprite9 && data8!={8'b0}) && condition) begin
										{red,green,blue}=data8;
										end	
								else if((sprite10 && data9!={8'b0}) && condition) begin
										{red,green,blue}=data9;
										end
										
								else if((sprite2 && data3!={8'b0}) && condition) begin
										{red,green,blue}=data3;
										end
								else if((sprite6 && collision && data5!={8'b0}) && condition ) begin
										{red,green,blue}=data5;
										end	
								else if((sprite7 && data6!={8'b0}) && condition) begin
										{red,green,blue}=data6;
										end	
								else if((sprite8 && data7!={8'b0}) && condition) begin
										{red,green,blue}=data7;
										end
								
								else if((vgadisp5 || vgadisp6 || vgadisp7 || vgadisp8 || (vgadisp19 && enable20) || vgadisp9 || (vgadisp10 && enable13) || (vgadisp11 && enable14) || (vgadisp12 && enable15) || (vgadisp13 && enable16) || (vgadisp14 && enable17) || (vgadisp15 && enable18) )  && condition && ~collision) begin
									red=3'b111;
									green=3'b000;
									blue=2'b00;
									end
								else if( vgadisp16 && game_over && ~lvl1 && ~lvl2 ) begin		
									red=3'b111;
									green=3'b000;
									blue=2'b00;
									end
									
								else if( vgadisp17 && lvl1 && ~game_over && ~lvl2 ) begin		
									red=3'b111;
									green=3'b000;
									blue=2'b00;
									end
									
									else if( vgadisp18 && lvl2 && ~game_over && ~lvl1) begin		
									red=3'b111;
									green=3'b000;
									blue=2'b00;
									end
									
									
									else if( TitleImage1 && ~game_over && ~lvl1 && ~lvl2 && start) begin		
									red=3'b111;
									green=3'b010;
									blue=2'b11;
									end
									
									else if( TitleImage2 && ~game_over && ~lvl1 && ~lvl2 && start) begin		
									red=3'b001;
									green=3'b001;
									blue=2'b11;
									end
									
									
									else begin
									red=3'b000;
									green=3'b000;
									blue=2'b00;
									end
		end




// module used for moving background

always@(posedge clk60)
			begin


							Xpos<=Xpos-2;
							Xposnext<=Xposnext-2;
							Xposnext1<=Xposnext1-2;
							Xposnext2<= Xposnext2-2;

							if(Xpos<-234)
							Xpos<=640;
							else if(Xposnext<-234)
							Xposnext<=640;
							else if(Xposnext1<-234)
							Xposnext1<=640;
							else if(Xposnext2<-234)
							Xposnext2<=640;



			end


endmodule

